<html>
<head>
<meta http-equiv="cache-control" content="no-cache, must-revalidate"/>
<meta http-equiv="Expires" Content="Mon, 25 May 2009 19:07:03 GMT">
<script type="text/javascript" src="../../src/gew.js"></script>

<script type="text/javascript">
var testScreenIdx = 0;
var elapseMls = 0;
var frameCount = 0;
function MyScreen(game)
{
  gewScreen.call(this, game);
  this.scene3d = new gewScene3D(this.game);
  this.onTick = function(mls)
  {
    var gl = this.game.gl;
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    this.scene3d.render();
    var root = this.scene3d.root;
    for (var i = 0; i < 10; i++) 
    {
      var node1 = root.children[i];
      var angle = node1.getRotateAngle();
      angle += mls * (0.05 / i);
      // node1.setRotateXYZ(0.,1.,0.,angle);
    }
    elapseMls += mls;
    frameCount++;
    if (elapseMls >= 2000) 
    {
      var fps = frameCount / 2;
      document.getElementById("fps").innerHTML = "Frame Rate:" + fps;
      elapseMls = 0;
      frameCount = 0;
    }
  }
  this.init = function()
  {
    var game = this.game;
    var gl = this.game.gl;
    var scene3d = this.scene3d;
    var root = scene3d.root;
    var camera_system = scene3d.camera_system;
    // init camera
    var cam = camera_system.createCamera(gewWorldCamera, null, root);
    cam.setDesiredPos(new gewVec3(0.0, 0.0, 40.0));
    cam.setPitch(-gewCONST_VALUES.PI / 4);
    cam.setFov(60.0);
    cam.setAspectRatio(1.0);
    cam.setNear(0.1);
    cam.setFar(100);
    cam.setViewport(0, 0, game.width, game.height);
    camera_system.setActiveCamera(cam);
    
    var shadow_effect = new gewCSMEffect(game, 512, 512);
    var effect = new gewPhongEffect(game);
    scene3d.addEffect(shadow_effect);
    scene3d.addEffect(effect);
    var shadow_material = new gewCSMMaterial(shadow_effect);
    var material = new gewPhongMaterial(effect);
    material.shadowMap = shadow_effect.shadowMap;
    var tmanager = this.game.textureManager;
    var m_manager = this.game.meshManager;
    tmanager.loadTexture("stone", "Resource/stone.jpg");
    //m_manager.loadObj("knight","Resource/knight.obj");
    m_manager.loadISM1("knight", "Resource/knight.ism1");
    material.texture = tmanager.getTexture("stone");
    var knight_mesh = m_manager.getMesh("knight");
    for (var i = 0; i < 10; i++) 
    {
      var temp = new gewTestNode();
      temp.setMesh(knight_mesh);
      temp.addMaterial(material);
      //temp.addMaterial(shadow_material);
      temp.setPositionXYZ(0, (5 - i) * 2, 0);
      //temp.setScaleXYZ(0.5, 0.5, 0.5);
      temp.debugName = "node"+i+"_0";
      root.appendChild(temp);
      for (var j = 0; j < 10; j++) 
      {
        var temp2 = new gewTestNode();
        temp2.setMesh(knight_mesh);
        temp2.addMaterial(material);
       // temp2.addMaterial(shadow_material);
        temp2.setPositionXYZ((5 - j) * 4, 0, 0);
        //temp2.setScaleXYZ(1.,1.,1.);
        temp.appendChild(temp2);
        temp2.debugName = "node"+i+"_"+j;
      }
    }
    var node1 = root.children[5];
    //node1.setScaleXYZ(1,1,1);
    //node1.setRotateXYZ(0.,1.,0.,45);
  }
  this.isDrag = false;
  this.onMouseDown = function(e)
  {
    this.isDrag = true;
  }
  this.onMouseMove = function(e)
  {
    if (this.isDrag) 
      console.log("move move");
  }
  this.onMouseUp = function(e)
  {
    this.isDrag = false;
  }
}

function main()
{
  var game = new gewGame("cavans_id");
  var blackScreen = new MyScreen(game);
  game.addScreen(blackScreen, testScreenIdx);
  game.setScreen(testScreenIdx);
  blackScreen.init();
  setInterval(function()
  {
    game.run()
  }, 10);
}
</script>
</head>
<body onload="main()" oncontextmenu="return false">
  <div style="text-align: center">
    <!-- this is the canvas that we render to -->
    <canvas id="cavans_id" width="640" height="480"></canvas>

    <!-- a convenient div to use to display log messages -->
    <div id="fps"></div>
    <a>Dont see anything? Get FireFox 3.7 and enable webGL feature ( about:config >> set webGL.. param to true )</a>
  </div>
</body>
</html>                  